﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.IDAL;
using AspNetCs.Model;
using System.Data;
using AspNetCs.Common;
namespace AspNetCs.SQLServerDAL
{
    public class Cemetery_DAL : ICemetery
    {

        #region ICemetery 成员

        public int Add(Cemetery_Model md)
        {
            int result = 0;
            db.ParameterArry[] par = new db.ParameterArry[11];
            par[0].dbType = SqlDbType.VarChar;
            par[0].objValue = md.Numid;
            par[0].parName = "@Numid";


            par[1].dbType = SqlDbType.VarChar;
            par[1].objValue = md.Names;
            par[1].parName = "@Names";

            par[2].dbType = SqlDbType.Int;
            par[2].objValue = md.Province;
            par[2].parName = "@province";

            par[3].dbType = SqlDbType.Int;
            par[3].objValue = md.City;
            par[3].parName = "@city";

            par[4].dbType = SqlDbType.Int;
            par[4].objValue = md.CreateId;
            par[4].parName = "@CreateId";

            par[5].dbType = SqlDbType.Int;
            par[5].objValue = md.TempleId;
            par[5].parName = "@TempleId";

            par[6].dbType = SqlDbType.Int;
            par[6].objValue = md.Ischeck;
            par[6].parName = "@Ischeck";


            par[7].dbType = SqlDbType.Int;
            par[7].objValue = md.MusicId;
            par[7].parName = "@MusicId";


            par[8].dbType = SqlDbType.Int;
            par[8].objValue = md.IsCommend;
            par[8].parName = "@IsCommend";


            par[9].dbType = SqlDbType.Int;
            par[9].objValue = md.Isdel;
            par[9].parName = "@Isdel";

            par[10].dbType = SqlDbType.DateTime;
            par[10].objValue = md.Addtime;
            par[10].parName = "@Addtime";
            string Sqlstr = "Insert into Cemetery(Numid,names,province,city,CreateId,TempleId,Ischeck,MusicId,IsCommend,Isdel,Addtime)";
            Sqlstr += " values(@Numid,@names,@province,@city,@CreateId,@TempleId,@Ischeck,@MusicId,@IsCommend,0,@Addtime)";
            db ConnInsert = new db();
            try
            {
                result = ConnInsert.ExcSql(Sqlstr, par);
            }
            catch
            {
                result = -1;
            }
            finally
            {
                ConnInsert.close();
            }
            return result;
        }

        public Cemetery_Model GetModel(DataRow dr)
        {
            Cemetery_Model model = new Cemetery_Model();
            if (dr["id"] != null) model.Id = StringHelper.isNumber_int(dr["id"].ToString());
            if (dr["Numid"] != null) model.Numid = dr["Numid"].ToString();
            if (dr["names"] != null) model.Names = dr["names"].ToString();
            if (dr["province"] != null) model.Province = StringHelper.isNumber_int(dr["province"].ToString());
            if (dr["city"] != null) model.City = StringHelper.isNumber_int(dr["city"].ToString());
            if (dr["CreateId"] != null) model.CreateId = StringHelper.isNumber_int(dr["CreateId"].ToString());
            if (dr["TempleId"] != null) model.TempleId = StringHelper.isNumber_int(dr["TempleId"].ToString());
            if (dr["Ischeck"] != null) model.Ischeck = StringHelper.isNumber_int(dr["Ischeck"].ToString());
            if (dr["MusicId"] != null) model.MusicId = StringHelper.isNumber_int(dr["MusicId"].ToString());
            if (dr["IsCommend"] != null) model.IsCommend = StringHelper.isNumber_int(dr["IsCommend"].ToString());
            if (dr["Isdel"] != null) model.Isdel = StringHelper.isNumber_int(dr["Isdel"].ToString());
            if (dr["Addtime"] != null) model.Addtime = StringHelper.isDatetime(dr["Addtime"].ToString());
            if (dr["hot"] != null) model.Hot = StringHelper.isNumber_int(dr["hot"].ToString());
            return model;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public DataTable GetDataTable(string filter)
        {
            DataTable dt = new DataTable("Cemetery_Dt");
            string wherestr = "";
            db Conn = new db();
            if (filter.Trim() != "")
            {
                wherestr = " Where " + filter;
            }
            dt = Conn.GetSql_DataTable("Select * from Cemetery " + wherestr);

            return dt;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public int delete(string filter)
        {
            int result = 0;
            if (filter.Trim() == "") return -2;//条件不能为空
            db conn = new db();
            try
            {
                result = conn.ExcSql("delete From Cemetery where " + filter);
            }
            catch
            {
                result = -1;//未知错误
            }
            finally
            {
                conn.close();
            }
            return result;
        }


        /// <summary>
        /// 设置推荐或者取消推荐
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="CancleOrIntro">1表示设置推荐 0表示取消推荐</param>
        /// <returns></returns>
        public int SetIntro(int Id, int CancleOrIntro)
        {
            int count = 0;
            string cmdText = "update Cemetery set IsCommend=@IsCommend where id=@id";
            db.ParameterArry[] paras = new db.ParameterArry[2];
            paras[0].dbType = SqlDbType.Int;
            paras[0].parName = "@IsCommend";
            paras[0].objValue = CancleOrIntro;

            paras[1].dbType = SqlDbType.Int;
            paras[1].parName = "@id";
            paras[1].objValue = Id;

            db dbclass = new db();
            count = dbclass.ExcSql(cmdText, paras);

            return count;
        }

        /// <summary>
        /// 审核或者取消审核
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="CancleOrIntro">1表示审核 0表示取消审核</param>
        /// <returns></returns>
        public int SetCheck(int Id, int CancleOrIntro)
        {
            int count = 0;
            string cmdText = "update Cemetery set Ischeck=@IsCheck where id=@id";
            db.ParameterArry[] paras = new db.ParameterArry[2];
            paras[0].dbType = SqlDbType.Int;
            paras[0].parName = "@IsCheck";
            paras[0].objValue = CancleOrIntro;

            paras[1].dbType = SqlDbType.Int;
            paras[1].parName = "@id";
            paras[1].objValue = Id;

            db dbclass = new db();
            count = dbclass.ExcSql(cmdText, paras);

            return count;
        }

        public void filterUpdate(string Upfilter, string filter)
        {
            db Conn = new db();
            if (filter + "" != "")
            {
                filter = "  where " + filter;
            }
            Conn.Execute_Noget("update Cemetery Set " + Upfilter + " " + filter);
        }

        /// <summary>
        /// 设置背景音乐
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="MusicId"></param>
        /// <returns></returns>
        public int SetMusic(int Id, int MusicId)
        {
            string cmdText = "update Cemetery set MusicId=@MusicId where Id=@ID";
            int count = 0;
            db.ParameterArry[] paras = new db.ParameterArry[2];

            paras[0].dbType = SqlDbType.Int;
            paras[0].parName = "@MusicId";
            paras[0].objValue = MusicId;

            paras[1].dbType = SqlDbType.Int;
            paras[1].parName = "@ID";
            paras[1].objValue = Id;

            db dbclass = new db();
            count = dbclass.ExcSql(cmdText, paras);
            return count;

        }

        #endregion
    }
}
